/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.eproject.logictics.dao.khoa;

import aptech.eproject.logictics.db.*;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.*;
import javax.annotation.*;
import javax.ejb.*;
import javax.persistence.*;

/**
 *
 * @author zZzShaRazZz
 */
@Stateless
@TransactionManagement(value = TransactionManagementType.CONTAINER)
public class UpdateProfile implements UpdateProfileRemote {

    @Resource
    private SessionContext context;
    @PersistenceContext(name = "LogicticsService")
    EntityManager em;

    public boolean update(String username, String fullname, Timestamp birthday, String gender, String address, int cityid, String phonenumber, String email) {
        try {
            Customer customer = em.find(Customer.class, username);
            customer.setFullName(fullname);
            customer.setDateOfBirth(birthday);
            customer.setGender(gender);
            customer.setAddress(address);
            City city = em.find(City.class, new Integer(cityid));
            customer.setCity(city);
            customer.setPhoneNumber(phonenumber);
            customer.setEmail(email);
            em.merge(customer);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public List getUserInfo(String username) {
        Query query = em.createQuery("SELECT c FROM Customer c WHERE c.userName=?1");
        query.setParameter("1", username);
        return query.getResultList();
    }

    public boolean update(String username, String newPass) {
        try {
            Customer customer = em.find(Customer.class, username);
            customer.setPassword(newPass);
            em.merge(customer);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
